package com.hongshan.wallet.base

import android.databinding.ViewDataBinding
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import me.goldze.mvvmhabit.base.BaseFragment


/**
 * Copyright Qiandai Ltd.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.core.main
 * @author   : Administrator
 * @data     : 2019/3/18
 * @email    : zhangxx@baihang-china.com
 */
abstract class MyFragment<V : ViewDataBinding,VM : MyViewModel>:BaseFragment<V,VM>() {

    val contentView by lazy { binding.root }
    var isVisable = false
    var isCreated = false

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = super.onCreateView(inflater, container, savedInstanceState)
        isCreated = true
        if (isVisable) {
            onVisible()
        }
        viewModel.initial()
        return view
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (userVisibleHint) {
            isVisable = true
            if (isCreated) onVisible()
        } else {
            isVisable = false
            if (isCreated) onInVisible()
        }
    }

    open fun onVisible() {}
    open fun onInVisible() {}
}